package fragments;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.junior.activities.R;

import java.util.List;

import basicas.AoClicarNoTimeListener;
import basicas.Time;
import util.TimeHttp;
import util.TimesAdapter;

/**
 * Created by Junior on 28/10/2014.
 */
public class ListaTimeFragment extends Fragment {

    List<Time> mtimes;
    ListView mListView;
    TimeTask mTimeTask;
    TextView mTxtMensagem;
    ProgressBar mProgressBar;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (mTimeTask == null) {
            if(temConexao()){
               mTimeTask = new TimeTask();
                mTimeTask.execute();
             }else {
                exibirMensagem(R.string.conexao);
            }
        }
        if (mTimeTask.getStatus() == AsyncTask.Status.RUNNING){
            mProgressBar.setVisibility(View.VISIBLE);
        } else {
            atualizarTela();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_time_lista,container,false);

        mListView = (ListView)view.findViewById(R.id.listView);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Time time = mtimes.get(i);
                notificarListener(time,true);

            }
        });

    return view;
    }

    public void notificarListener(Time time, boolean b) {
        if (getActivity() instanceof AoClicarNoTimeListener) {
            AoClicarNoTimeListener listener =
                    (AoClicarNoTimeListener) getActivity();
            listener.aoClicarNoTime(time, b);
        }
    }

    private void exibirMensagem(int mensagem) {
        mTxtMensagem.setVisibility(View.VISIBLE);
        mTxtMensagem.setText(mensagem);
        mProgressBar.setVisibility(View.GONE);
    }

    public void atualizarTela() {
        TimesAdapter adapter = new TimesAdapter(getActivity(), mtimes);
        mListView.setAdapter(adapter);
        mTxtMensagem.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.GONE);
    }

    private boolean temConexao() {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return (info != null && info.isConnected());
    }

    class TimeTask extends AsyncTask<Void, Void, List<Time>>{
        @Override
        protected List<Time> doInBackground(Void... params) {
            return TimeHttp.downloadTime();
        }

        @Override
        protected void onPostExecute(List<Time> times) {
            super.onPostExecute(times);

            if(times != null) {
                mtimes = times;
                atualizarTela();
                notificarListener(times.get(0),false);
            }
            else{
                exibirMensagem(R.string.time);
            }

        }
    }
}
